// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/common/file/file.h>
#include <pollux/testing/common/faulty_file_system_operations.h>

namespace kumo::pollux::tests::utils {

class FaultyReadFile : public ReadFile {
 public:
  FaultyReadFile(
      const std::string& path,
      std::shared_ptr<ReadFile> delegatedFile,
      FileFaultInjectionHook injectionHook,
      melon::Executor* executor);

  ~FaultyReadFile() override{};

  uint64_t size() const override {
    return delegatedFile_->size();
  }

  std::string_view pread(
      uint64_t offset,
      uint64_t length,
      void* buf,
      filesystems::File::IoStats* stats = nullptr) const override;

  uint64_t preadv(
      uint64_t offset,
      const std::vector<melon::Range<char*>>& buffers,
      filesystems::File::IoStats* stats = nullptr) const override;

  uint64_t memoryUsage() const override {
    return delegatedFile_->memoryUsage();
  }

  bool shouldCoalesce() const override {
    return delegatedFile_->shouldCoalesce();
  }

  std::string getName() const override {
    return delegatedFile_->getName();
  }

  uint64_t getNaturalReadSize() const override {
    return delegatedFile_->getNaturalReadSize();
  }

  bool hasPreadvAsync() const override {
    if (executor_ != nullptr) {
      return true;
    }
    return delegatedFile_->hasPreadvAsync();
  }

  melon::SemiFuture<uint64_t> preadvAsync(
      uint64_t offset,
      const std::vector<melon::Range<char*>>& buffers,
      filesystems::File::IoStats* stats = nullptr) const override;

 private:
  const std::string path_;
  const std::shared_ptr<ReadFile> delegatedFile_;
  const FileFaultInjectionHook injectionHook_;
  melon::Executor* const executor_;
};

class FaultyWriteFile : public WriteFile {
 public:
  FaultyWriteFile(
      const std::string& path,
      std::shared_ptr<WriteFile> delegatedFile,
      FileFaultInjectionHook injectionHook);

  ~FaultyWriteFile() override{};

  void append(std::string_view data) override;

  void append(std::unique_ptr<melon::IOBuf> data) override;

  void write(const std::vector<iovec>& iovecs, int64_t offset, int64_t length)
      override;

  void truncate(int64_t newSize) override;

  void flush() override;

  void setAttributes(
      const melon::F14FastMap<std::string, std::string>& attributes) override;

  melon::F14FastMap<std::string, std::string> getAttributes() const override;

  void close() override;

  uint64_t size() const override;

  const std::string getName() const override;

 private:
  const std::string path_;
  const std::shared_ptr<WriteFile> delegatedFile_;
  const FileFaultInjectionHook injectionHook_;
};

} // namespace kumo::pollux::tests::utils
